#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAXN 105
#define MAXM 5000

struct edge {
    int u, v, w;
    bool operator < (const edge &rhs) const {
        return w < rhs.w;
    }
};

int N, M;
edge G[MAXM];

int kruskal(int mw) {
    int big, small, cnt=0;
    int mi = 0;
    int FL[MAXN];
    for (int i=1; i<=N; i++) {
        FL[i] = i;
    }
    while (cnt < N-1) {
        int minw = 0x3F3F3F3F;
        int ei;
        if (mi >= M)
            return -1;
        edge e = G[mi];
        if (FL[e.u] != FL[e.v] && e.w < minw && e.w >= mw) {
            ei = mi;
            minw = e.w;
        } else {
            mi++;
            continue;
        }
        mi++;
        if (cnt == 0) {
            small = minw;
        }
        if (cnt == N-2) {
            big = minw;
        }
        cnt++;
        int fleu = FL[G[ei].u];
        int flev = FL[G[ei].v];
        for (int i=1; i<=N; i++) {
            if (FL[i] == flev)
                FL[i] = fleu;
        }
    }
    return big - small;
}


int main()
{
    while (~scanf("%d%d", &N, &M) && N) {
        for (int i=0; i<M; i++) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            G[i] = edge{u, v, w};
        }
        sort(G, G+M);
        bool found = false;
        int prev = -1;
        int min = 0x3F3F3F3F;
        for (int i=0; i<M; i++) {
            if (G[i].w != prev) {
                prev = G[i].w;
                int a = kruskal(G[i].w);
                if (a >= 0) {
                    found = true;
                    if (a < min) {
                        min = a;
                    }
                } else {
                    break;
                }
            }
        }
        if (!found) {
            printf("-1\n");
        } else {
            printf("%d\n", min);
        }
    }
    return 0;
}
